package view;

import java.awt.BorderLayout;
import java.util.Hashtable;
import java.util.ResourceBundle;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * @author Andrea
 * 
 */
public class SettingsPieces {
	// Load resource bundles
	private static ResourceBundle myViewLabels = ResourceBundle
			.getBundle("resources.englishView");
	private static ResourceBundle myViewSizes = ResourceBundle
			.getBundle("resources.viewSizes");

	private JPanel sliderPanel;
	private JSlider mySlider;
	private int framesPerSecond;
	private Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
	private int numberOfTicks = Integer.parseInt(myViewSizes
			.getString("AnimationSliderTicks"));

	public JPanel sliderComponent() {
		// Initialize panel to hold slider
		sliderPanel = new JPanel(new BorderLayout());
		JSlider slider = makeSlider(Integer.parseInt(myViewSizes
				.getString("SliderMin")), Integer.parseInt(myViewSizes
				.getString("SliderMax")), Integer.parseInt(myViewSizes
				.getString("SliderInitialValue")));

		sliderPanel.add(slider, BorderLayout.CENTER);
		sliderPanel.setBorder(BorderFactory.createTitledBorder(myViewLabels
				.getString("AnimationPanel")));

		return sliderPanel;
	}

	// Make slider with specified minimum, maximum, and starting values
	public JSlider makeSlider(int minSpeed, int maxSpeed, int startingSpeed) {
		mySlider = new JSlider(minSpeed, maxSpeed, startingSpeed);
		mySlider.setOrientation(JSlider.HORIZONTAL);

		mySlider.setMajorTickSpacing((maxSpeed - minSpeed) / numberOfTicks);
		mySlider.setPaintTicks(true);

		// Makes labels that correspond to certain values on the slider and
		// displays them
		labelTable.put(new Integer(minSpeed), new JLabel(myViewLabels
				.getString("AnimationSliderMin")));
		labelTable.put(new Integer(maxSpeed), new JLabel(myViewLabels
				.getString("AnimationSliderMax")));
		mySlider.setLabelTable(labelTable);
		mySlider.setPaintLabels(true);
		mySlider.addChangeListener(new SliderListener());
		return mySlider;
	}
	
	class SliderListener implements ChangeListener {
	    public void stateChanged(ChangeEvent e) {
	        JSlider source = (JSlider)e.getSource();
	        if (!source.getValueIsAdjusting()) {
	            framesPerSecond = (int)source.getValue();
	        }    
	    }
	}
	
	public int getAnimationSpeed()
	{
		return framesPerSecond;
	}
}